﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows.Input;
using System.Windows.Interop;
using System.Xml.Linq;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.ViewModels
{
    using Models;
    using Songhay.Models;
    using Songhay.Mvvm.Models;
    using Songhay.Net;
    using Songhay.Mvvm.ViewModels;
    using Songhay.Xml;
    using System.Windows;

    /// <summary>
    /// The view-model definition for <c>FlowView.xaml</c>.
    /// </summary>
    public class ClientViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FlowViewModel"/> class.
        /// </summary>
        public ClientViewModel()
        {
            if(base.IsInDesignMode)
            {
                this.SetDesignTime();
                return;
            }

            #region commanding:

            base.CommandDictionary.Add("#togglefullembed",
            ()=>
            {
                var message = new LightNavigationMessage<string>(".", "#togglefullembed");
                Messenger.Default.Send(message);
            });

            this.IndexItemCommand = new RelayCommand<string>(param =>
            {

                if(this.CommandDictionary.ContainsKey(param))
                {
                    this.CommandDictionary[param].Invoke();
                    return;
                }

                var message = new LightNavigationMessage<string>(param, "IndexItemCommand");
                Messenger.Default.Send(message);
            });

            #endregion

            #region messaging:

            var action = new Action<object, DownloadStringCompletedEventArgs>(
            (o, args) =>
            {
                if(args.Error == null)
                {
                    var xd = XDocument.Parse(args.Result);
                    var data = OpmlUtility.GetDocument(xd.Root, OpmlUtility.rx);

                    this.IndexCreatedOn = data.OpmlHead.DateCreated.Value;

                    this.Outlines = data.OpmlBody.Outlines;
                    this.IndexModifiedOn = data.OpmlHead.DateModified.Value;
                    this.IndexOwnerEmailAddress = data.OpmlHead.OwnerEmail;
                    this.IndexOwnerName = data.OpmlHead.OwnerName;
                    this.IndexTitle = data.OpmlHead.Title;
                }
            });

            Messenger.Default.Register(this, new Action<GenericMessage<ClientSettings>>(
            message =>
            {
                HttpVerbAsyncUtility.Get(message.Content.OpmlIndex, action);
            }));

            #endregion
        }

        /// <summary>
        /// Gets or sets the index created on.
        /// </summary>
        /// <value>The index created on.</value>
        public DateTime IndexCreatedOn
        {
            get { return this._indexCreatedOn; }
            set
            {
                this._indexCreatedOn = value;
                base.RaisePropertyChanged("IndexCreatedOn");
            }
        }

        /// <summary>
        /// Gets the index item command.
        /// </summary>
        /// <value>The index item command.</value>
        public ICommand IndexItemCommand { get; private set; }

        /// <summary>
        /// Gets or sets the index items.
        /// </summary>
        /// <value>The index items.</value>
        public IEnumerable<OpmlOutline> Outlines
        {
            get { return this._indexItems; }
            set
            {
                this._indexItems = value;
                base.RaisePropertyChanged("Outlines");
            }
        }

        /// <summary>
        /// Gets or sets the modified on.
        /// </summary>
        /// <value>The modified on.</value>
        public DateTime IndexModifiedOn
        {
            get { return this._indexModifiedOn; }
            set
            {
                this._indexModifiedOn = value;
                base.RaisePropertyChanged("IndexModifiedOn");
            }
        }

        /// <summary>
        /// Gets or sets the index owner email address.
        /// </summary>
        /// <value>The index owner email address.</value>
        public string IndexOwnerEmailAddress
        {
            get { return this._indexOwnerEmailAddress; }
            set
            {
                this._indexOwnerEmailAddress = value;
                base.RaisePropertyChanged("IndexOwnerEmailAddress");
            }
        }

        /// <summary>
        /// Gets or sets the name of the index owner.
        /// </summary>
        /// <value>The name of the index owner.</value>
        public string IndexOwnerName
        {
            get { return this._indexOwnerName; }
            set
            {
                this._indexOwnerName = value;
                base.RaisePropertyChanged("IndexOwnerName");
            }
        }

        /// <summary>
        /// Gets or sets the index title.
        /// </summary>
        /// <value>The index title.</value>
        public string IndexTitle
        {
            get { return this._indexTitle; }
            set
            {
                this._indexTitle = value;
                base.RaisePropertyChanged("IndexTitle");
            }
        }

        void SetDesignTime()
        {
            this.DisplayName = "Flow Design-time";
            this.IndexCreatedOn = DateTime.Now;
            this.Outlines = new OpmlOutline[]
                {
                    new OpmlOutline
                    {
                        Text = "Design-time item text 1",
                        Url="#",

                        Outlines = new OpmlOutline[]
                        {
                            new OpmlOutline{ Text = "Child Item 1", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 1b", Url = "#" }
                        }
                    },
                    new OpmlOutline
                    {
                        Text = "Design-time item text 2",
                        Url="#",

                        Outlines = new OpmlOutline[]
                        {
                            new OpmlOutline{ Text = "Child Item 1", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 2", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 3", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 4", Url = "#" }
                        }
                    },
                    new OpmlOutline
                    {
                        Text = "Design-time item text 3",
                        Url="#",

                        Outlines = new OpmlOutline[]
                        {
                            new OpmlOutline{ Text = "Child Item 1", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 2", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 3", Url = "#" }
                        }
                    },
                    new OpmlOutline
                    {
                        Text = "Design-time item text 4",
                        Url="#",

                        Outlines = new OpmlOutline[]
                        {
                            new OpmlOutline{ Text = "Child Item 1", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 2", Url = "#" },
                            new OpmlOutline{ Text = "Child Item 2b", Url = "#" }
                        }
                    }
                };
            this.IndexModifiedOn = DateTime.Now;
            this.IndexOwnerEmailAddress = "rasx@SonghaySystem.com";
            this.IndexOwnerName = "Bryan Wilhite";
            this.IndexTitle = "My Design-time Index";
        }

        DateTime _indexCreatedOn;
        IEnumerable<OpmlOutline> _indexItems;
        DateTime _indexModifiedOn;
        string _indexOwnerEmailAddress;
        string _indexOwnerName;
        string _indexTitle;
    }
}
